package cn.iocoder.yudao.module.system.job.chaoshun;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.chaoshun.utils.DxUtils;
import cn.iocoder.yudao.module.system.controller.admin.chaoshun.vo.fsdx.TemplateParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 判断朝顺网站是否可以整场连接 Job
 *
 * @author j-sentinel
 */
@Component
@Slf4j
public class ChaoshunCheckJob implements JobHandler {

    @Override
    @TenantIgnore
    public String execute(String param) {
        if (!isUrlAccessible("https://chaoshunwuliu.com:9999/chaoshun/chaoshun")) {
            List<String> sjhmList = new ArrayList<>();
            List<String> signList = new ArrayList<>();
            String templateCode = "SMS_476830062";
            List<TemplateParamVO> paramList = new ArrayList<>();
            // 使用formatter来格式化date
            sjhmList.add("16651117024");
            signList.add("邳州市北斗恒昇物流");
            TemplateParamVO templateParamVO = new TemplateParamVO();
//            templateParamVO.setCommodity(spDO.getSpmc());
            templateParamVO.setCommodity("朝顺快递");
            templateParamVO.setPlace("文苑路与中山路交叉路口立交桥下面最里面(朝顺物流快递点)");
            templateParamVO.setCode("1234");
            templateParamVO.setSjhm("18068485869");
            paramList.add(templateParamVO);
            DxUtils.fsdx(sjhmList, signList, templateCode, paramList);
        }
        log.info("已检测朝顺网址");
        return "已检测朝顺网址";
    }

    public static boolean isUrlAccessible(String urlString) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("HEAD"); // 使用HEAD方法进行请求，不获取内容，仅获得响应头信息
            connection.setConnectTimeout(3000); // 设置连接超时时间
            connection.setReadTimeout(3000); // 设置读取超时时间
            int responseCode = connection.getResponseCode();

            // 判断响应码是否表示成功（200-399 范围内）
            return (200 <= responseCode && responseCode <= 399);
        } catch (Exception e) {
            // 发生异常时，URL不可访问
            return false;
        }
    }

}
